package com.woniuxy.kaer.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.service.entity.*;
import com.woniuxy.kaer.service.mapper.*;
import com.woniuxy.kaer.service.param.AddServiceRealParam;
import com.woniuxy.kaer.service.param.UpdateServiceRealParam;
import com.woniuxy.kaer.service.service.ServiceRealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 程诗洋
 * @since 2025年02月17日
 */
@Service
public class ServiceRealServiceImpl extends ServiceImpl<ServiceRealMapper, ServiceReal> implements ServiceRealService {

    @Resource
    private ServiceBaseMapper serviceBaseMapper;
    @Resource
    private ServiceRealMapper serviceRealMapper;
    @Resource
    private ServiceOrderMapper serviceOrderMapper;
    @Resource
    private ServiceProcessesMapper  serviceProcessesMapper;
    @Resource
    private ServiceRealDetailMapper serviceRealDetailMapper;


    @Override
    public void updateServiceReal(UpdateServiceRealParam param) throws Exception {
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(param.getServiceOrderId());
        if(null==serviceOrder){
            throw new RuntimeException("订单不存在");
        }
        if(Data.SERVICE_ORDER_STATE_ACCEPT!=serviceOrder.getServiceOrderState()){
            throw new RuntimeException("订单状态异常");
        }
        ServiceReal serviceReal = serviceRealMapper.selectById(param.getServiceRealId());
        if(null==serviceReal){
            throw new RuntimeException("服务订单项目不存在");
        }
        //取消其中服务，订单总价变化
        serviceOrder.setTotalPrice(serviceOrder.getTotalPrice().subtract(serviceReal.getRealHourlyRate()));
        serviceOrderMapper.updateById(serviceOrder);
        serviceRealMapper.deleteById(param.getServiceRealId());
        QueryWrapper<ServiceRealDetail> serviceRealDetailQueryWrapper = new QueryWrapper<>();
        serviceRealDetailQueryWrapper.eq("service_real_id",param.getServiceRealId());
        List<ServiceRealDetail> serviceRealDetails = serviceRealDetailMapper.selectList(serviceRealDetailQueryWrapper);
        for (ServiceRealDetail serviceRealDetail : serviceRealDetails) {
            serviceRealDetailMapper.deleteById(serviceRealDetail.getServiceRealDetailId());
        }
       // serviceRealDetailMapper.delete(serviceRealDetailQueryWrapper);
        QueryWrapper<ServiceProcesses> serviceProcessesQueryWrapper = new QueryWrapper<>();
        serviceProcessesQueryWrapper.eq("service_order_id",param.getServiceOrderId());
        List<ServiceProcesses> serviceProcesses = serviceProcessesMapper.selectList(serviceProcessesQueryWrapper);
        Integer serialNumber = 0;
        for (ServiceProcesses serviceProcesses1 : serviceProcesses) {
            if(serviceProcesses1.getServiceRealId()==param.getServiceRealId()){
                serialNumber=serviceProcesses1.getSerialNumber();
                serviceProcessesMapper.deleteById(serviceProcesses1.getProcessesId());
            }
        }
        for (ServiceProcesses serviceProcess : serviceProcesses) {
            if(serviceProcess.getSerialNumber()>serialNumber){
                serviceProcess.setSerialNumber(serviceProcess.getSerialNumber()-1);
                serviceProcessesMapper.updateById(serviceProcess);
            }
        }

    }

    @Override
    public void AddServiceReal(AddServiceRealParam param) throws Exception {
        ServiceReal serviceReal = BeanUtil.toBean(param, ServiceReal.class);
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(param.getServiceOrderId());
        if(null==serviceOrder){
            throw new RuntimeException("订单不存在");
        }

        serviceOrder.setTotalPrice(serviceOrder.getTotalPrice().add(param.getRealHourlyRate()));
        serviceRealMapper.insert(serviceReal);
        serviceOrderMapper.updateById(serviceOrder);
        QueryWrapper<ServiceProcesses> serviceProcessesQueryWrapper = new QueryWrapper<>();
        serviceProcessesQueryWrapper.eq("service_order_id",param.getServiceOrderId());
        List<ServiceProcesses> serviceProcesses = serviceProcessesMapper.selectList(serviceProcessesQueryWrapper);
        Integer serialNumber = serviceProcesses.size()+1;
        ServiceProcesses serviceProcesses1 = new ServiceProcesses();
        serviceProcesses1.setServiceRealId(serviceReal.getServiceRealId());
        serviceProcesses1.setProcessesName(serviceReal.getServiceRealName());
        serviceProcesses1.setState(0);
        serviceProcesses1.setProcessesName(serviceReal.getServiceRealName());
        serviceProcesses1.setSerialNumber(serviceProcesses.size()+1);
        serviceProcesses1.setServiceOrderId(serviceReal.getServiceOrderId());
        serviceProcesses1.setCurrentOperator(param.getCurrentOperator());
        serviceProcessesMapper.insert(serviceProcesses1);

    }
}
